package com.ganmiao.web.portal.controller;


import com.ganmiao.server.supplier.entity.Tender;
import com.ganmiao.server.supplier.service.TenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tender")
public class TenderController {

    @Autowired
    private TenderService tenderService;

    // Create a tender
    @PostMapping("/insert")
    public int tenderCreate(@RequestBody Tender tender){
        List<Tender> tenders = tenderService.getAllTenders();
        // check if id already exists
        for (Tender t: tenders) {
            if (tender.getId() == t.getId()) {
                return 0;
            }
        }
        return tenderService.tenderCreate(tender);
    }

    @GetMapping("/get")
    public Tender tendersGet(@RequestParam Integer id){
        return tenderService.tendersGet(id);
    }

    @GetMapping("/getAll")
    public List<Tender> getAllTenders() {
        return tenderService.getAllTenders();
    }

    @PutMapping("/put/{id}")
    public void tendersUpdate(@PathVariable Integer id, @RequestBody Tender tender) {
        if (tender.getTender_Status() != 1) {
            tenderService.tendersUpdate(id, tender);
        }
    }

    @DeleteMapping("/delete/{id}")
    public void tendersDelete(@PathVariable Integer id) {
        if (tenderService.tendersGet(id).getTender_Status() != 1) {
            tenderService.tendersDelete(id);
        }
    }
}
